define(function(require, exports, module) {

	var page = {
		"pageIndex": 1,
		"pageSize": 20,
		"pageMax": 1,
		"totalCount": 0,
		"dataState": true,
		"viewid": "",
		"tempid": "",
		"dataModel": {},
		"isCache": false,
		"dataItemId": "id",
		"dataItem": "items",
		"noneMsg": "",
		"dataCache": [],
		"fun": {},
		"loadEnd": null,
		"reset": function() {

			page.pageIndex = 1;
			page.dataState = true;
			page.pageMax = 1;

			page.dataCache = [];

		},
		"init": function(config, muiconfig, pfun, pdata) {

			this.viewid = config.id;
			this.tempid = config.tempid;
			this.isCache = (config.isCache === true ? true : false);
			this.pageSize = config.pageSize > 0 ? config.pageSize : this.pageSize;
			this.noneMsg = config.noneMsg;

			if(config.dataItem) {
				this.dataItem = config.dataItem;
			}
			if(config.dataItemId) {
				this.dataItemId = config.dataItemId;
			}
			if(config.loadEnd) {
				this.loadEnd = config.loadEnd;
			}

			this.dataModel = pdata;
			this.fun = pfun;

			var $obj = $("#" + config.id);

			$obj.wrap("<div id='pullrefresh' class='mui-scroll-wrapper'><div id='nr' class='mui-scroll'></div></div>");

			var top = $(".pop-tab").length > 0 ? "50px" : "0";

			$obj.css({
				"margin-top": top
			}).addClass("mui-table-view"); //.addClass("mui-table-view-chevron");

			$('.mui-content').prepend("<div class='tipsConInit'></div>");

			var it = muiconfig;

			it.pullRefresh = {
				container: "#pullrefresh",
				down: {
					height: 40, //可选.默认50.触发上拉加载拖动距离
					callback: page.pulldownRefresh,
					contentdown: "下拉可以刷新", //可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
					contentover: "释放立即刷新", //可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
					contentrefresh: "正在刷新...", //可选，正在刷新状态时，下拉刷新控件上显示的标题内容
				},
				up: {
					height: 40, //可选.默认50.触发上拉加载拖动距离
					contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
					contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
					callback: page.pullupRefresh
				}
			};

			mui.init(it);

			$(".mui-pull-top-pocket").css({
				"top": top
			});

			return this;
		},
		"pulldownRefresh": function() {

			page.reset();

			page.dataLoad(this.dataModel, 'down');

		},
		"pullupRefresh": function() {

			page.pageIndex++;
			page.dataState = false;

			page.dataLoad(this.dataModel, 'up');

		},
		"dataLoad": function(currDataModel, act) {

			var option = {};

			if(currDataModel != undefined) {
				page.dataModel = currDataModel;

				page.reset();
			}

			if(act != 'down' && act != 'up') {
				mui('#pullrefresh').pullRefresh().refresh(true);
			}

			page.dataModel.pageSize = page.pageSize;
			page.dataModel.pageIndex = page.pageIndex;

			var contentTips = (act == 'init' ? "initContentTips" : "searchContentTips");

			page.fun(page.dataModel, option).
			done(function(data) {

				var totalCountNum = data.totalCount;
				if(act == 'init' && totalCountNum === 0) {
					$('.mui-content > .tipsConInit').html(page.noneMsg);
				} else {
					$('.mui-content > .tipsConInit').html("");
				}

				page.totalCount = data.totalCount;
				page.pageMax = Math.ceil(totalCountNum / page.pageSize);

				if(totalCountNum < 1) {
					$('.mui-content').addClass(contentTips);
					$('#pullrefresh').hide();
				} else {
					$('#pullrefresh').show();

					if(act == 'down') {
						mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
					} else if(act == 'up') {
						mui('#pullrefresh').pullRefresh().endPullupToRefresh((page.pageMax < page.pageIndex));
					}

					$('.mui-content').removeClass(contentTips);
				}

				if(page.isCache) {
					for(var i = 0; i < data[page.dataItem].length; i++) {
						page.dataCache[data[page.dataItem][i][page.dataItemId]] = data[page.dataItem][i];
					}
				}

				var items = data.items;
				$.each(items, function(i, v) {
					//console.log(v);
					if(v.serialDate) {
						v.serialDate = v.serialDate.substring(0, 10);
					}
					if(v.dataCreated) {
						v.dataCreated = v.dataCreated.substring(0, 10);
					}
				});

				var html = template(page.tempid, data);

				//console.log(html);
				if(page.dataState === true) {

					$("#" + page.viewid).html(html);

				} else {

					$("#" + page.viewid).append(html);
				}

				var statusColor = require("statusIdColor");
				statusColor($(".statusId"));

				if(page.loadEnd) {
					page.loadEnd();
				}

			}).fail(function(data) {
				mui.alert('<ul class="mui-row pop-tabmain"><li class="mui-col-xs-6 mui-col-sm-6"style="text-align: right;padding-right: 20px;"><a class="yuanico"><span>×</span></a></li><li class="mui-col-xs-6 mui-col-sm-6" style="text-align: left;line-height: 46px;">' + data.message + '</li></ul>', ' ', function() {}, 'div');
			});

		}
	};

	module.exports = page;

});